package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.security.ShiroUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectCustomerTrack;
import com.abvnet.project.water.project.domain.ProjectInfo;
import com.abvnet.project.water.project.domain.ProjectStatus;
import com.abvnet.project.water.project.service.IProjectCustomerTrackService;
import com.abvnet.project.water.project.service.IProjectInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户跟踪信息操作处理
 * 
 * @author duj
 * @date 2019-08-17
 */
@Controller
@RequestMapping("/project/projectCustomerTrack")
public class ProjectCustomerTrackController extends BaseController {
    private String prefix = "project/projectCustomerTrack";
	
	@Autowired
	private IProjectCustomerTrackService projectCustomerTrackService;

    @Autowired
    private IProjectInfoService projectInfoService;
	
	@RequiresPermissions("project:projectCustomerTrack:list")
	@GetMapping()
	public String projectCustomerTrack()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询客户跟踪列表
	 */
	@RequiresPermissions("project:projectCustomerTrack:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectInfo projectInfo) {
		if (!ShiroUtils.getSysUser().isAdmin()) { // 非管理员，只有数据可见中的用户能看到
			projectInfo.setCreateBy(ShiroUtils.getLoginName());
			projectInfo.setVisibleUserId(String.valueOf(ShiroUtils.getUserId()));
		}
        startPage();
        List<ProjectInfo> list = projectInfoService.selectProjectInfoList(projectInfo);
        return getDataTable(list);
	}

    @RequiresPermissions("project:projectCustomerTrack:list")
    @GetMapping("/track/{projectId}")
    public String projectTrack(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectId", projectId);
        return prefix + "/track";
    }

    @RequiresPermissions("project:projectCustomerTrack:list")
    @PostMapping("/track")
    @ResponseBody
    public TableDataInfo track(ProjectCustomerTrack projectCustomerTrack) {
        startPage();
        List<ProjectCustomerTrack> list = projectCustomerTrackService.selectProjectCustomerTrackList(projectCustomerTrack);
        return getDataTable(list);
    }
	
	/**
	 * 新增客户跟踪
	 */
	@GetMapping("/add/{projectId}")
	public String add(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("projectId", projectId);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存客户跟踪
	 */
	@RequiresPermissions("project:projectCustomerTrack:add")
	@Log(title = "客户跟踪", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectCustomerTrack projectCustomerTrack) {
        ProjectInfo projectInfo = projectInfoService.selectProgressCodeById(projectCustomerTrack.getProjectId());
//        if (StringUtils.equals(ProjectStatus.GUIDE.getCode(),projectInfo.getProgressCode())) {
//            projectInfo.setProgressCode(ProjectStatus.TRACK.getCode());
//            projectInfoService.updateProjectInfo(projectInfo);
//        }
		if (Integer.parseInt(projectInfo.getProgressCode()) < Integer.parseInt(ProjectStatus.TRACK.getCode())) {
			projectInfo.setProgressCode(ProjectStatus.TRACK.getCode());
			projectInfoService.updateProjectInfo(projectInfo);
		}
		return toAjax(projectCustomerTrackService.insertProjectCustomerTrack(projectCustomerTrack));
	}

	/**
	 * 修改客户跟踪
	 */
	@GetMapping("/edit/{trackId}")
	public String edit(@PathVariable("trackId") Long trackId, ModelMap mmap) {
		ProjectCustomerTrack projectCustomerTrack = projectCustomerTrackService.selectProjectCustomerTrackById(trackId);
		mmap.put("projectCustomerTrack", projectCustomerTrack);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存客户跟踪
	 */
	@RequiresPermissions("project:projectCustomerTrack:edit")
	@Log(title = "客户跟踪", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectCustomerTrack projectCustomerTrack) {
		return toAjax(projectCustomerTrackService.updateProjectCustomerTrack(projectCustomerTrack));
	}
	
	/**
	 * 删除客户跟踪
	 */
	@RequiresPermissions("project:projectCustomerTrack:remove")
	@Log(title = "客户跟踪", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(projectCustomerTrackService.deleteProjectCustomerTrackByIds(ids));
	}
	
}
